%{
    This file is part of StemCellQC, a video bioinformatics software
    toolkit for analysis of phase contrast microscopy videos.
    Copyright 2013-2015 Vincent On. [vincenton001-at-gmail.com]

    StemCellQC is free software: you can redistribute it and/or 
    modify it under the terms of the GNU General Public License as 
    published by the Free Software Foundation, either version 3 of the 
    License, or (at your option) any later version.

    StemCellQC is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with StemCellQC.  If not, see <http://www.gnu.org/licenses/>.
%}

function tracks = check_split( frame, nt_idx, k, tracks, cc, temp )            
%check to see if there is an overlap with a previous region
curr_reg = cc{ frame }.PixelIdxList{ temp( k, 1 )};
overlap = [];

%check all regions in the previous frame
for reg = 1 : cc{ frame - 1 }.NumObjects
    prev_reg = cc{ frame - 1 }.PixelIdxList{ reg };
    
    %count number of overlapping pixels
    overlap_count = numel( intersect( curr_reg, prev_reg ));
    
    if overlap_count > 0
        %check if overlapped region is used by any existing track
        for curr_tracks = 1 : nt_idx - 1
            if length(tracks( curr_tracks ).cc_label) > 2
                if reg == tracks( curr_tracks ).cc_label( end - 2 );
                    overlap_track = curr_tracks;
                    break
                end
            end
        end
        
        split = [overlap_track nt_idx];
        
        %check area of the overlapping regions
        curr_feat = regionprops( cc{frame - 0}, 'Centroid', 'Area' );%% test 1 additional frame back
        prev_feat = regionprops( cc{frame - 1}, 'Centroid', 'Area' );
        
        prev_area = prev_feat( tracks( overlap_track ).cc_label( end - 2 )).Area;
        curr_area = [];
        
        for k = split
            curr_area( end + 1 ) = curr_feat( tracks(k).cc_label( end - 1 )).Area;
        end
        t_curr_area = sum( curr_area );
        
        a_range = .2;   %percent deviation for area
        area_check = 0;
        cen_check = 0;
        if ((( 1 - a_range ) * t_curr_area ) < prev_area) && (prev_area < (( 1 + a_range ) * t_curr_area ))
            area_check = 1;
        end
        
        %check bounding box of the overlapping region
        mov_range = .2;
        if area_check == 1
            prev_cen = prev_feat( tracks( overlap_track ).cc_label( end - 2 )).Centroid;
            curr_cen = [];
            for k = split
                curr_cen( end + 1, : ) = curr_feat( tracks(k).cc_label( end - 1 )).Centroid;
            end
            
            b = bounding_box_points( curr_cen );
            
            if prev_cen(2) > (( 1 - mov_range ) * b(1)) ...
                    && prev_cen(2) < (( 1 + mov_range ) * b(2)) ...
                    && prev_cen(1) > (( 1 - mov_range ) * b(3)) ...
                    && prev_cen(1) < (( 1 + mov_range ) * b(4))
            
                cen_check = 1;
            end
        end
        
                %if split conditions are met, mark merged tracks and frame in which
        %they merged.
        
        if area_check == 1 && cen_check == 1
            %if new track is larger than overlapping track, switch
            %connection of tracks
            [~,largest_track] = max( curr_area );
            if largest_track == 2
                temp_data = tracks( split( 1 )).start_frame;
                tracks( split( 1 )).start_frame  = tracks( split( 2 )).start_frame;
                tracks( split( 2 )).start_frame = temp_data;
                
                tracks( split( 2 )).cc_label = [ tracks( split( 1 )).cc_label( 1 : end - 2 ), tracks( split( 2 )).cc_label ];
                tracks( split( 1 )).cc_label( 1 : end - 2 ) = [];
            end
            
            for k = split
%                 if k ~= merge(largest_track);
%                     tracks(k).cc_label(end) = [];
%                     tracks(k).complete = 0;
%                 end
                
                tracks(k).tracks_merged = split;
                tracks(k).split_frame = frame;
            end
        end
    end
end





